//
//  GHRecommendViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHRecommendViewController.h"
#import "GHRecomendModel.h"
#import "GHRecomendTableViewCell.h"
@interface GHRecommendViewController ()
@property(nonatomic, assign) NSInteger                                             page;
@property(nonatomic, strong) NSMutableArray<GHRecomendModel*>                     *dataSouce;
@property(nonatomic, strong) GHNoResultView                                         *noResultView;
@end

@implementation GHRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐明细";
    [self configSubView];
    self.dataSouce = [NSMutableArray new];
    //无数据view
    self.noResultView = [[GHNoResultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350) title:@"暂无推荐" buttonTitle:@"快去推荐吧" buttonHandle:^(NSString *titleString) {
        
    }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self sendMyrecomendRequestWithPage:self.page];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    //头部
    //标题
    CGFloat lableWith = UIScreenWidth / 4;
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 50)];
    titleView.backgroundColor = [UIColor whiteColor];
    UILabel * numLable = [self defultLableTitle:@"用户名" showRigthLine:YES];
    numLable.st_left = 0;
    [titleView addSubview:numLable];
    UILabel * allUserLable = [self defultLableTitle:@"注册时间" showRigthLine:YES];
    allUserLable.st_left = 0 + lableWith;
    [titleView addSubview:allUserLable];
    UILabel * todayLable = [self defultLableTitle:@"累积消费" showRigthLine:YES];
    todayLable.st_left = 0 + 2 *lableWith;
    [titleView addSubview:todayLable];
    UILabel * allLable = [self defultLableTitle:@"累积明细" showRigthLine:NO];
    allLable.st_left = 0 + 3 *lableWith;
    [titleView addSubview:allLable];
    self.tableView.tableHeaderView = titleView;
    
}
- (UILabel*)defultLableTitle:(NSString*)title showRigthLine:(BOOL)showRigthLine{
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 4, 50)
                                                     text:title
                                                textColor:GH_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    if (showRigthLine) {
        [titleLable st_showRightLine:50];
    }
    return titleLable;
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GHRecomendTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHRecomendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHRecomendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)sendMyrecomendRequestWithPage:(NSInteger)page{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@(page) forKey:@"page"];
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/personal/recommended_details"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                [SVProgressHUD dismiss];
                                                
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"recommended_data"]) {
                                                     NSArray * dicArray = dataDic[@"recommended_data"];
                                                     for (NSDictionary *recomDic in dicArray) {
                                                         GHRecomendModel  * model = [GHRecomendModel mj_objectWithKeyValues:recomDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView reloadData];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 [self.tableView.mj_header endRefreshing];
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [SVProgressHUD dismiss];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
